#ifndef IMAGES_MANAGER_H
#define IMAGES_MANAGER_H

#include <map>

#include <QImage>

#include "image_utils.h"

using namespace std;

class ImagesManager
{
public:
    // images ids
    static int CHECK;
    static int BUTTON_NORMAL;
    static int BUTTON_HOVER;
    static int BUTTON_PRESSED;
    static int BACKGROUND;
    static int VOLUME;
    static int ON;
    static int OFF;
    static int UP;
    static int DOWN;
    static int WORK_IN_PROGRESS;

    // public functions
    static ImagesManager* GetInstance()
    {
        if(instance == 0)
        {
            instance = new ImagesManager();
        }
        return instance;
    }

    QImage* GetImage(int id)
    {
        QImage* toRet = 0;
        Images::iterator i = images.find(id);
        if(i != images.end())
        {   // the image has been found
            toRet = (*i).second;
        }else
        {   // we must load the image
            toRet = LoadImage(id);
            images[id] = toRet;
        }
        return toRet;
    }

private:
    typedef map< int, QImage* > Images;

    ImagesManager()
    {
        // loads the button images
        QImage img(":/button.png");
        int h = img.height()/3;

        QImage pressed = img.copy(0, 0, img.width(), h);
        //ImageUtils::AddOutline(pressed);
        images[BUTTON_PRESSED] = new QImage(pressed);

        QImage hover = img.copy(0, h, img.width(), h);
        //ImageUtils::AddOutline(hover);
        images[BUTTON_HOVER] = new QImage(hover);

        QImage normal = img.copy(0, 2 * h, img.width(), h);
        //ImageUtils::AddOutline(normal);
        images[BUTTON_NORMAL] = new QImage(normal);
    }

    ~ImagesManager()
    {
        for(Images::iterator i=images.begin(); i!=images.end(); ++i)
        {
            delete ((*i).second);
        }
    }

    QImage* LoadImage(int id)
    {
        QImage* img = 0;
        switch(id)
        {
        case 0: // check sign
            img = new QImage(":/check.png");
            break;
        case 4: // background image
            img = new QImage(":/background.png");
            break;
        case 5:
            img = new QImage(":/volume.png");
            break;
        case 6:
            img = new QImage(":/on.png");
            break;
        case 7:
            img = new QImage(":/off.png");
            break;
        case 8:
            img = new QImage(":/up.png");
            break;
        case 9:
            img = new QImage(":/down.png");
            break;
        case 10:
            img = new QImage(":/wip.png");
        }
        return img;
    }

    Images images;
    static ImagesManager* instance;
};

#endif // IMAGES_MANAGER_H
